﻿using System;
using System.Disposables;
using System.Windows;
using System.Windows.Controls;
using SoftSource.Common;
using SoftSource.MVVMC;
using SoftSource.Reactive;
using SoftSource.Timecard.Resources;
using SoftSource.Timecard.ViewModelInterfaces;
using SoftSource.Timecard.Web.Data;

namespace SoftSource.Timecard.Views
{
    public sealed partial class ProjectMembershipEditorView : UserControl, IView<IProjectMembershipEditorViewModelConsume>, IDisposable
    {
        #region Fields

        private CompositeDisposable compositeDisposable;

        #endregion Fields

        public ProjectMembershipEditorView()
        {
            InitializeComponent();

            this.compositeDisposable = new CompositeDisposable();

            Loaded += new RoutedEventHandler(ProjectMembershipEditorView_Loaded);
        }

        #region Events

        void ProjectMembershipEditorView_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.ViewModel != null)
            {
                this.Busy.BusyContent = ApplicationStrings.LoadingMessage;
                this.Busy.IsBusy = true;

                this.ViewModel.Load // Load the data required to use this ViewModel
                    .Nullable()
                    .Subscribe(unit => this.Busy.IsBusy = false);
            }

            var beginningEditProjectDisposable = this.ProjectUsersDataGrid
                .WhenBeginningEdit()
                .Subscribe(args =>
                {
                    args.EventArgs.Cancel = true;
                    this.OpenEditor(args.EventArgs.Row.DataContext as ProjectMember);
                });

            var beginningEditSourceDisposable = this.SourceUsersDataGrid
                .WhenBeginningEdit()
                .Subscribe(args =>
                {
                    args.EventArgs.Cancel = true;
                    this.ViewModel.AddToProject(this.SourceUsersDataGrid.SelectedItem);
                });

            this.compositeDisposable.Add(beginningEditProjectDisposable);
            this.compositeDisposable.Add(beginningEditSourceDisposable);
        }

        private void RemoveFromProject_Click(object sender, RoutedEventArgs e)
        {
            this.ViewModel.RemoveFromProject(this.ProjectUsersDataGrid.SelectedItem);
        }

        private void AddToProject_Click(object sender, RoutedEventArgs e)
        {
            this.ViewModel.AddToProject.TryInvoke(this.SourceUsersDataGrid.SelectedItem);
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.ViewModel.Completed.TryInvoke();
        }

        private void EditMembership_Click(object sender, RoutedEventArgs e)
        {
            ProjectMember member = this.ProjectUsersDataGrid.SelectedItem as ProjectMember;
            this.OpenEditor(member);
        }

        #endregion Events

        #region Methods

        private void OpenEditor(ProjectMember member)
        {
            if (member != null)
            {
                ChildWindow cw = new ChildWindow();

                UIElement view = this.ViewModel.CreateProjectMemberEditor.TryInvoke(member, cw.Close);

                cw.Content = view;
                cw.Show();
            }
        }

        #endregion Methods

        #region IView<IProjectMemberEditorViewModelConsume> Members

        public IProjectMembershipEditorViewModelConsume ViewModel
        {
            get { return this.DataContext as IProjectMembershipEditorViewModelConsume; }
            set { this.DataContext = value; }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            this.compositeDisposable.Dispose();
        }

        #endregion
    }
}
